package com.lee.demo1.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lee.demo1.business.ArticleBusiness;
import com.lee.demo1.domain.Article;
import com.lee.demo1.domain.Picture;
import com.lee.demo1.util.pager.Pager;
import com.lee.demo1.util.pager.PagerHelper;


@Controller
@RequestMapping("article")
public class ArticleController {
	
	private static Logger logger = LoggerFactory.getLogger(ArticleController.class);
	
	@Resource
	private ArticleBusiness articleBusiness;
	
	@RequestMapping  
    public String load(Model model){
		model.addAttribute("categories", articleBusiness.getCategories());
        return "admin/article/form";
    }
	
	@RequestMapping(params = "method=save")
	public String save(Article article,RedirectAttributes redirectAttributes){
		
		redirectAttributes.addFlashAttribute("message", article.getTitle());
		//判断更新或新增
		if(null!=article.getId()){
			articleBusiness.updateArticle(article);
			return "redirect:/article?method=edit&id=" + article.getId();
		}else{
			articleBusiness.addArticle(article);
			return "redirect:/article";
		}
		
		
	}
	/**
	 * 进入内容管理列表页面
	 * @param model
	 * @param article
	 * @param currentPage
	 * @param pageMethod
	 * @return
	 */
	@RequestMapping(params = "method=list")
	public String list(Model model, Article article, Integer currentPage , String pageMethod ){
		
		model.addAttribute("categories", articleBusiness.getCategories());
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("categoryId", article.getCategoryId());
		int totalRows = articleBusiness.getAeticleCount(map);
		model.addAttribute("categoryId",article.getCategoryId());
		if(totalRows>0){
			Pager pager = PagerHelper.getPager(currentPage,totalRows,pageMethod);
			map.put("begin", pager.getStartRow());
			map.put("size", pager.getPageSize());
			
			List<Article> articleList = articleBusiness.getInstructonsList(map);
			model.addAttribute("articles", articleList);
			
			model.addAttribute("pager",pager);
		}
		
		//logger.debug("------------------" + article.getCategoryId());
		return "admin/article/list";
	}
	
	@RequestMapping( params = "method=edit" )
	public String update(Model model, int id){
		model.addAttribute("categories", articleBusiness.getCategories());
		model.addAttribute("article",articleBusiness.getArticle(id));
        return "admin/article/form";
	}
	
	/**
	 * 删除内容 ajax
	 * @param id
	 * @return
	 */
	@RequestMapping( params = "method=delete" )
	@ResponseBody
	public String delete(int id){
		articleBusiness.deleteArticle(id);
		return "success";
	}
	
	@RequestMapping( params = "method=updateDes" )
	@ResponseBody
	public String updateDes(int id, String description){
		logger.debug("---------------------" + id);
		logger.debug("---------------------" + description);
		
		int updateCount = articleBusiness.updatePicture(id, description, null);
		
		return "success";
		
	}
	@RequestMapping( params = "method=updateSort" )
	@ResponseBody
	public String updateSort(int id, Integer sort){
		logger.debug("---------------------" + id);
		logger.debug("---------------------" + sort);
		
		int updateCount = articleBusiness.updatePicture(id, null, sort);
		
		return "success";
		
	}
	@RequestMapping( params = "method=editPic" )
	public String editPic(Model model,int articleId){
		List<Picture> pics = articleBusiness.getPictures(articleId);
		model.addAttribute("pictures" , pics);
		model.addAttribute("articleId",articleId);
		return "admin/article/editPic";
	}
	
	@RequestMapping( params = "method=uploadPic" )
	public String uploadPic(RedirectAttributes redirectAttributes, HttpServletRequest request, int articleId){
		articleBusiness.uploadPicture(request,articleId);
		redirectAttributes.addFlashAttribute("message", 1);
		return "redirect:/article?method=editPic&articleId=" + articleId;
	}
	
	
}
